package io.renren.modules.reception.controller;

import java.util.*;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.annotation.NoRepeatSubmit;
import io.renren.common.utils.FastIDUtil;
import io.renren.modules.constants.RedisConstants;
import io.renren.modules.reception.entity.TStoreOpenDeskEntity;
import io.renren.modules.reception.entity.vo.OrderInfo;
import io.renren.modules.reception.init.TableThemeCache;
import io.renren.modules.reception.service.TStoreOpenDeskService;
import io.renren.modules.store.entity.TStoreDiningTableEntity;
import io.renren.modules.store.service.TStoreDiningTableService;
import io.renren.modules.sys.controller.AbstractController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.reception.entity.TStoreReserveDeskEntity;
import io.renren.modules.reception.service.TStoreReserveDeskService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author steakliu
 * @email 2319492349@qq.com
 * @date 2021-03-02 23:17:55
 */
@RestController
@RequestMapping("reception/tstorereservedesk")
public class TStoreReserveDeskController extends AbstractController {
    @Autowired
    private TStoreReserveDeskService tStoreReserveDeskService;
    @Autowired
    private TStoreOpenDeskService storeOpenDeskService;
    @Autowired
    private TStoreDiningTableService diningTableService;
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("reception:tstorereservedesk:list")
    public R list(@RequestParam Map<String, Object> params){
        params.put("sellerId",getSeller());
        //PageUtils page = tStoreReserveDeskService.queryPage(params);
        Page<TStoreReserveDeskEntity> page = tStoreReserveDeskService.listReserveDesk(params);
        return R.ok().put("page", page);
    }

    /**
     * 开台下单
     */
    @NoRepeatSubmit
    @RequestMapping("/openDesk")
    public R openDesk(@RequestBody TStoreReserveDeskEntity deskEntity){
        //开台(存redis)
        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOrderId(FastIDUtil.fastId());
        orderInfo.setEatNumer(deskEntity.getPeopleNum());
        orderInfo.setDiningTableNo(deskEntity.getDiningTableNo());
        orderInfo.setId(deskEntity.getDeskId());
        orderInfo.setOpenTime(new Date());
        orderInfo.setTotalMoney(deskEntity.getTotalMoney());
        orderInfo.setFoodVos(deskEntity.getFoodVos());
        Map<String, Object> map = BeanUtil.beanToMap(orderInfo);
        redisTemplate.opsForHash().putAll(RedisConstants.OPEN_DESK_PREFIX+deskEntity.getDeskId() + RedisConstants.OPEN_DESK_SYMBOL+getSeller(),map);

        //改变餐台状态
        TStoreDiningTableEntity diningTableEntity = new TStoreDiningTableEntity();
        diningTableEntity.setStatus(2);
        diningTableEntity.setStyle(TableThemeCache.get(getSeller()).getEatColor());
        diningTableEntity.setMoney(deskEntity.getTotalMoney());
        diningTableEntity.setTime(new Date());
        diningTableEntity.setId(deskEntity.getDeskId());
        diningTableService.updateById(diningTableEntity);
        
        //改变预定状态(已到店)
        TStoreReserveDeskEntity storeReserveDeskEntity = new TStoreReserveDeskEntity();
        storeReserveDeskEntity.setStatus(2);
        storeReserveDeskEntity.setId(deskEntity.getId());
        tStoreReserveDeskService.updateById(storeReserveDeskEntity);

        return R.ok();
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("reception:tstorereservedesk:info")
    public R info(@PathVariable("id") Long id){
		TStoreReserveDeskEntity tStoreReserveDesk = tStoreReserveDeskService.getById(id);

        return R.ok().put("tStoreReserveDesk", tStoreReserveDesk);
    }

    @RequestMapping("/cancelReserve")
    public R cancelReserve(@RequestParam Long id,@RequestParam Integer status){
        TStoreReserveDeskEntity storeReserveDeskEntity = new TStoreReserveDeskEntity();
        storeReserveDeskEntity.setId(id);
        storeReserveDeskEntity.setStatus(status);
        tStoreReserveDeskService.updateById(storeReserveDeskEntity);
        return R.ok();
    }

    /**
     * 保存
     */
    @NoRepeatSubmit
    @RequestMapping("/save")
    @RequiresPermissions("reception:tstorereservedesk:save")
    public R save(@RequestBody TStoreReserveDeskEntity tStoreReserveDesk){
        tStoreReserveDesk.setSellerId(getSeller());
        if (tStoreReserveDesk.getFoodVos().size() > 0){
            Object json = JSONObject.toJSON(tStoreReserveDesk.getFoodVos());
            tStoreReserveDesk.setFoodList(json.toString());
        }

        tStoreReserveDesk.setTime(new Date());
        List<TStoreReserveDeskEntity> list = new ArrayList<>();
        for (int i = 0 ; i < tStoreReserveDesk.getDeskIdList().size() ; i++){
            TStoreReserveDeskEntity storeReserveDeskEntity = new TStoreReserveDeskEntity();
            tStoreReserveDesk.setDeskId(tStoreReserveDesk.getDeskIdList().get(i));
            tStoreReserveDesk.setReserveNo(FastIDUtil.fastId());
            BeanUtil.copyProperties(tStoreReserveDesk,storeReserveDeskEntity);
            list.add(storeReserveDeskEntity);
        }
		tStoreReserveDeskService.saveBatch(list);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("reception:tstorereservedesk:update")
    public R update(@RequestBody TStoreReserveDeskEntity tStoreReserveDesk){
		tStoreReserveDeskService.updateById(tStoreReserveDesk);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("reception:tstorereservedesk:delete")
    public R delete(@RequestBody Long[] ids){
		tStoreReserveDeskService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
